// pages/subpage/visitor.js
var ipconfig = "http://192.168.3.133:8080"//服务器ip地址
Page({

  /**
   * 页面的初始数据
   */
  data: {
      site:'',
      display:'block',
    visitor_name:'',
    visitor_people:'',
    visitor_name1: '',
    visitor_people1: '',
    visitor_name2: '',
    visitor_people2: '',
    boloks: 'none',
  },
  adds:function(){
    wx.navigateTo({
      url: 'adds',
    })
  },
  adds1:function(){
    wx.redirectTo({
      url: '../index/home-page'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    wx.request({
      url: ipconfig+"/Wxtext",
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success:function(res){
        that.setData({
          site:"桐梓林一号"+res.data[1]+"栋"+res.data[2]+"单元"+res.data[0]+"号",
        })
      }
    }),
    wx.request({
      url: ipconfig+"/Wxvisitors",
        method: "post",
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': wx.getStorageSync("session")
        },
        success:function(res){
          console.log(res.data);
          if(res.data!=null&&res.data.length>0){
            var visitorName = [];
            var visitor_people = [];
            for (var i = 0; i < res.data.length; i++) {
              visitorName.push(res.data[i].visitor_name)
              visitor_people.push(res.data[i].visitor_number)
            }
            if(res.data!=null&&res.data.length==1){
              that.setData({
                visitor_name: "访客姓名：" + visitorName[0] + "，",
                visitor_people: "访客人数: " + visitor_people[0],
                boloks: 'block',
                display: 'none',
              })
            }else if(res.data.length==2){
              that.setData({
                visitor_name: "访客姓名：" + visitorName[0] + "，",
                visitor_people: "访客人数: " + visitor_people[0],
                visitor_name1: "访客姓名：" + visitorName[1] + "，",
                visitor_people1: "访客人数: " + visitor_people[1],
                boloks: 'block',
                display: 'none',
              })
            }else if(res.data.length==3){
              that.setData({
                visitor_name: "访客姓名：" + visitorName[0] + "，",
                visitor_people: "访客人数: " + visitor_people[0],
                visitor_name1: "访客姓名：" + visitorName[1] + "，",
                visitor_people1: "访客人数: " + visitor_people[1],
                visitor_name2: "访客姓名：" + visitorName[2] + "，",
                visitor_people2: "访客人数: " + visitor_people[2],
                boloks: 'block',
                display: 'none',
              })
            }


          }else{
              that.setData({
                boloks: 'none',
                display: 'block',
              })
          }
         
        }
    }),
    wx.request({
      url: ipconfig+"/Wxtry",
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function (res) {
        console.log(res.data);
        if (res.data != null && res.data.length > 0) {
          var visitorName = [];
          var visitor_people = [];
          for (var i = 0; i < res.data.length; i++) {
            visitorName.push(res.data[i].visitor_name)
            visitor_people.push(res.data[i].visitor_number)
          }
          if (res.data.length == 1) {
            that.setData({
              visitor_name: "访客姓名：" + visitorName[0] + "，",
              visitor_people: "访客人数: " + visitor_people[0],
              boloks: 'block',
              display: 'none',
            })
          } else if (res.data.length == 2) {
            that.setData({
              visitor_name: "访客姓名：" + visitorName[0] + "，",
              visitor_people: "访客人数: " + visitor_people[0],
              visitor_name1: "访客姓名：" + visitorName[1] + "，",
              visitor_people1: "访客人数: " + visitor_people[1],
              boloks: 'block',
              display: 'none',
            })
          } else if (res.data.length == 3) {
            that.setData({
              visitor_name: "访客姓名：" + visitorName[0] + "，",
              visitor_people: "访客人数: " + visitor_people[0],
              visitor_name1: "访客姓名：" + visitorName[1] + "，",
              visitor_people1: "访客人数: " + visitor_people[1],
              visitor_name2: "访客姓名：" + visitorName[2] + "，",
              visitor_people2: "访客人数: " + visitor_people[2],
              boloks: 'block',
              display: 'none',
            })
          }


        } else {
          that.setData({
            boloks: 'none',
            display: 'block',
          })
        }

      }
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  visitou_time:function(inputTime) {
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  },
})